<?php
	require_once('../config.php');
	include_once("../database/DataBase.php");
	include_once("../include/function.php");
				
	session_start();
	checkLogin();

	$cid = $_SESSION["company_id"];
	$database = New DataBase();
	$database->open_connection();
	$category = $database->getProductCategory($cid, $_SESSION["language"]);

?>

<html>
	<head>
		<title>CMS - Item</title>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
		<?php
			include_once("../include/includeHeader.php");
		?>
		<link rel="Stylesheet" type="text/css" href="<?echo $config["root"];?>/css/jHtmlArea.css" />
		<script type="text/javascript" src="<?echo $config["root"];?>/js/jHtmlArea"></script>
		<script type="text/javascript">
		
			$(document).ready(function(){
				$('#promo_desc').htmlarea();
			});
		
			$(function() {
				$( "#start_date" ).datepicker({
					dateFormat: 'dd/mm/yy',
					changeMonth: true,
					changeYear: true
				});
			});
			
			$(function() {
				$( "#end_date" ).datepicker({
					dateFormat: 'dd/mm/yy',
					changeMonth: true,
					changeYear: true
				});
			});
		
		
			function checkSubmit(object){
				var message = "";
				
				if(object.promo_code.value == ""){
					$("#promo_code").addClass("error");
					message = message+ "<li>Please input Promotion Code</li>";
				}
				else{
					$("#promo_code").removeClass("error");
				}
				
				if($('input[name=promo_type]:radio:checked').val() == "A"){
					if(object.A1.value == ""){
						$("#A1").addClass("error");
						message = message+ "<li>Please input discount number</li>";
					}
					else if(isNaN(object.A1.value)){
						$("#A1").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else if(object.A1.value <= 0){
						$("#A1").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else{
						$("#A1").removeClass("error");
					}
				}
				
				if($('input[name=promo_type]:radio:checked').val() == "B"){
					if(object.B1.value == ""){
						$("#B1").addClass("error");
						message = message+ "<li>Please input discount number</li>";
					}
					else if(isNaN(object.B1.value)){
						$("#B1").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else if(object.B1.value <= 0){
						$("#B1").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else{
						$("#B1").removeClass("error");
					}
					
					if(object.B2.value == ""){
						$("#B2").addClass("error");
						message = message+ "<li>Please input discount number</li>";
					}
					else if(isNaN(object.B2.value)){
						$("#B2").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else if(object.B2.value <= 0){
						$("#B2").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else{
						$("#B2").removeClass("error");
					}
					
				}
				
				if($('input[name=promo_type]:radio:checked').val() == "C"){
					if(object.C1.value == ""){
						$("#C1").addClass("error");
						message = message+ "<li>Please input discount number</li>";
					}
					else if(isNAN(object.C1.value)){
						$("#C1").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else if(object.C1.value <= 0){
						$("#C1").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else{
						$("#C1").removeClass("error");
					}
					
					if(object.C2.value == ""){
						$("#C2").addClass("error");
						message = message+ "<li>Please input discount number</li>";
					}
					else if(isNAN(object.C2.value)){
						$("#C2").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else if(object.C2.value <= 0){
						$("#C2").addClass("error");
						message = message+ "<li>Please input correct discount</li>";
					}
					else{
						$("#C2").removeClass("error");
					}
				}
				
				var startDate;
				var endDate;
				
				startDate = new Date(object.start_date.value);
				endDate = new Date(object.end_date.value);
				
				if(object.start_date.value == "" || object.end_date.value == ""){
					$("#start_date").addClass("error");
					$("#end_date").addClass("error");
					message = message+ "<li>Please input Start Date and End Date</li>";
				}
				else if(startDate > endDate){
					$("#start_date").addClass("error");
					$("#end_date").addClass("error");
					message = message+ "<li>Please input correct Start Date and End Date</li>";
				}
				else{
					$("#start_date").removeClass("error");
					$("#end_date").removeClass("error");
				}
				
				if(message == ""){
				
					switch($('input[name=promo_type]:radio:checked').val()){
						case "A" :
							$("#field_a").val(object.A1.value);
							break;
						case "B" :
							$("#field_a").val(object.B1.value);
							$("#field_b").val(object.B2.value);
							break;
						case "C" :
							$("#field_a").val(object.C1.value);
							$("#field_b").val(object.C2.value);
							break;
					}
					
					document.new_promotion.submit();
				}
			}
			
			function selectRadio(value){
				document.getElementById(value).checked = true;
			}
		</script>
	</head>
	<body>
		<?php
			include_once("../include/top_navigation.php");
		?>
		<div Class="Middle">
			<div class="LeftMenu">
				<?php include_once("../include/left_menu.php"); ?>
			</div>
			<div id="RightContent" >
				<form name="new_promotion" method="POST" action="../include/discount_add_process.php" enctype="multipart/form-data">
				<table>
					<tr>
						<td>Promotion Code : </td>
						<td><input type="text" name="promo_code" id="promo_code" value="" /> </td>
					</tr>
					<? for($i=0;$i<$_SESSION["num_of_lang"];$i++){ ?>
					<tr>
						<td>Description : </td>
						<td>
							<textarea name="promo_desc[]" id="promo_desc" rows="10" class="promo_desc" cols="100"></textarea>
						</td>
					</tr>
					<? } ?>
					<tr>
						<td><input type="radio" name="promo_type" value="A" id="A" >Type A</td>
						<td>
							<input type="text" name="A1" id="A1" value="" onclick="javascript: selectRadio('A');" /> % off discount.
						</td>
					</tr>
					<tr>
						<td><input type="radio" name="promo_type" value="B" id="B" >Type B</td>
						<td>
							Buy <input type="text" name="B1" id="B1" value="" onclick="javascript: selectRadio('B');"/> get <input type="text" name="B2" id="B2" value="" onclick="javascript: selectRadio('B');" /> free.
						</td>
					</tr>
					<tr>
						<td><input type="radio" name="promo_type" value="C" id="C" >Type C</td>
						<td>
							Buy <input type="text" name="C1" id="C1" value="" onclick="javascript: selectRadio('C');"/> get <input type="text" name="C2" id="C2" value="" onclick="javascript: selectRadio('C');" /> % off discount.
						</td>
					</tr>
					<tr>
						<td>Start Date : </td>
						<td>
							<input type="text" name="start_date" id="start_date" value="" />
						</td>
					</tr>
					<tr>
						<td>End Date : </td>
						<td>
							<input type="text" name="end_date" id="end_date" value="" />
						</td>
					</tr>
					<tr>
						<td>Limit : </td>
						<td><input type="text" name="limit" id="limit" value="" /></td>
					</tr>
					<tr>
						<td colspan="2"><input type="button" name="button" value="Submit" onclick="javascript: checkSubmit(document.new_promotion);"/></td>
					</tr>
				</table>
				
				<input type="hidden" name="field_a" id="field_a" value="" />
				<input type="hidden" name="field_b" id="field_b" value="" />
				<input type="hidden" name="field_c" id="field_c" value="" />
				<input type="hidden" name="cid" id="cid" value="<? echo $cid;?>" />
				</form>
			</div>
		</div>
		<?php
			include_once("../include/footer.php");
		?>
	</body>
</html>
<?
	mysql_free_result($status);
	mysql_free_result($category);
	$database->close_connection();
?>
